package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Company;
import com.example.demo.entity.JobPosition;
import com.example.demo.entity.User;
import com.example.demo.mapper.JobPositionMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.CompanyService;
import com.example.demo.mapper.CompanyMapper;
import com.example.demo.vo.UserVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 你爸爸
* @description 针对表【company(公司表)】的数据库操作Service实现
* @createDate 2023-12-12 18:37:09
*/
@Service
@Slf4j
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService{

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JobPositionMapper jobPositionMapper;

    @Override
    public List<Company> selectByUserId(Integer id) {
        return companyMapper.selectByUserId(id);
    }


    @Override
    public IPage<Company> selectPage(Page<Company> page) {
        IPage<Company> companyPage = companyMapper.selectPage(page, null);  // 执行分页查询
        List<Company> companyList = companyPage.getRecords();  // 获取分页对象中的数据列表
        for (Company company:companyList){
            QueryWrapper<User> userWrapper=new QueryWrapper<>();
            userWrapper.eq("id",company.getUserId());
            List<User> userList=userMapper.selectList(userWrapper);
            List<UserVo> listUserVo = new ArrayList<>();
            for (User record : userList) {
                listUserVo.add(UserVo.builder()
                        .id(record.getId())
                        .avatar(record.getAvatar())
                        .name(record.getName())
                        .account(record.getAccount())
                        .roleId(record.getRoleId())
                        .phone(record.getPhone())
                        .mailbox(record.getMailbox())
                        .wechat(record.getWechat())
                        .qq(record.getQq())
                        .loginTime(record.getLoginTime())
                        .logoutTime(record.getLogoutTime())
                        .createTime(record.getCreateTime())
                        .build());
            }
            company.setUserList(listUserVo);
            QueryWrapper<JobPosition> wrapper=new QueryWrapper<>();
            wrapper.eq("company_id",company.getId());
            List<JobPosition> list=jobPositionMapper.selectList(wrapper);
            company.setJobPositionList(list);
        }

        companyPage.setRecords(companyList);
        return companyPage;
    }
}




